package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.common.reponse.JsonResult;
import cn.tedu._05mvcboot02.mappper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.AddUserDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@Api(tags = "用户模块")
@Slf4j
//@Controller
@RestController
@RequestMapping(value = "/v1/users/")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "添加用户")
    @PostMapping(value = "insert")
    public JsonResult addUser(AddUserDTO addUserDTO) {
        log.debug("addUserDTO="+addUserDTO);
        User user = new User();
        BeanUtils.copyProperties(addUserDTO, user);
        user.setCreated(new Date());
        log.debug("user="+user);
        userMapper.insertUser(user);
        return JsonResult.ok();
    }
    @ApiOperation(value ="用户列表")
    @GetMapping(value = "userList")
    public JsonResult userList(){
        return JsonResult.ok(userMapper.selectUserList());
    }

    @ApiOperation(value = "删除用户")
    @GetMapping(value = "delete")
    @ApiImplicitParam(name = "id",value = "用户ID",required = true,dataType = "int")
    public JsonResult deleteUser(int id){
        log.debug("id="+id);
        if (id<0){
            throw new IllegalArgumentException("ID不能小于0");
        }
        userMapper.deleteUserById(id);
        return JsonResult.ok();
    }
    @ApiOperation(value = "修改用户")
    @PostMapping(value = "update")
    public JsonResult updateUser(User user){
        log.debug("user="+user);
        userMapper.updateUser(user);
        return JsonResult.ok();
    }
    @ApiOperation(value = "获取指定用户")
    @GetMapping(value = "{id}")
    @ApiImplicitParam(name = "id",value = "用户ID",required = true,dataType = "int")
    public JsonResult getUserById(@PathVariable int id){
        log.debug("id="+id);
        return JsonResult.ok(userMapper.selectUserById(id));
    }
}
